package mbp.gui.panels;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Color;

import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.JLabel;

import mbp.base.ServiceType;
import mbp.core.Resource;
import mbp.core.ServiceTypeBuilder;
import mbp.core.Service;
import mbp.gui.frames.MbpManagerGui;

import javax.swing.JTextField;
import org.apache.log4j.Logger;

public class ServiceBuilderGui extends JPanel implements ActionListener
{
	
	private MbpManagerGui mbpManagerGui;
	private HashMap<String, Service> servicesStore;
	private Service service;
	private JComboBox cmbServices;
	private JComboBox cmbTypes;
	private JButton btnSend;
	private JButton btnCreate;
	private JTextField txtAgentName;
	
	private Logger logger = Logger.getLogger(this.getClass());
	private ServiceEditPanel serviceEditPanel;

	/**
	 * Create the panel.
	 */
	public ServiceBuilderGui(MbpManagerGui mbpManagerGui)
	{
		this.mbpManagerGui = mbpManagerGui;
		servicesStore = new HashMap<String, Service>();
		setLayout(null);
		
		JPanel panel_0 = new JPanel();
		panel_0.setLayout(null);
		panel_0.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Service Type Fom XML", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_0.setBounds(10, 11, 234, 70);
		add(panel_0);
		
		cmbTypes = new JComboBox();
		cmbTypes.setBounds(10, 39, 125, 20);
		panel_0.add(cmbTypes);
		
		btnCreate = new JButton("Create");
		btnCreate.setName("CREATE");
		btnCreate.setFont(new Font("Arial", Font.PLAIN, 11));
		btnCreate.setBounds(145, 36, 80, 23);
		panel_0.add(btnCreate);
		
		JLabel lblSeviceType = new JLabel("Sevice type");
		lblSeviceType.setBounds(10, 22, 100, 14);
		panel_0.add(lblSeviceType);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Active service", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_1.setBounds(256, 11, 144, 70);
		add(panel_1);
		
		cmbServices = new JComboBox();
		cmbServices.setBounds(10, 39, 125, 20);
		panel_1.add(cmbServices);
		
		JLabel lblServicesList = new JLabel("Services list");
		lblServicesList.setBounds(10, 24, 110, 14);
		panel_1.add(lblServicesList);
		
		btnCreate.addActionListener(this);
		
		serviceEditPanel = new ServiceEditPanel();
		serviceEditPanel.setBounds(10, 93, 674, 265);
		add(serviceEditPanel);
		
		JPanel panel_3 = new JPanel();
		panel_3.setLayout(null);
		panel_3.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Send service", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_3.setBounds(412, 11, 272, 70);
		add(panel_3);
		
		txtAgentName = new JTextField();
		txtAgentName.setColumns(10);
		txtAgentName.setBounds(10, 39, 120, 20);
		panel_3.add(txtAgentName);
		
		btnSend = new JButton("Send");
		btnSend.setFont(new Font("Arial", Font.PLAIN, 11));
		btnSend.setBounds(140, 38, 91, 23);
		panel_3.add(btnSend);
		btnSend.addActionListener(this);
		
		JLabel label = new JLabel("Agent name");
		label.setFont(new Font("Dialog", Font.PLAIN, 12));
		label.setBounds(10, 24, 80, 14);
		panel_3.add(label);
		
		cmbServices.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				int index = cmbServices.getSelectedIndex();
				String key = (String)cmbServices.getItemAt(index);
				loadService(servicesStore.get(key));
			}
		});

	}
	
	public ArrayList<Service> getServicesList()
	{
		ArrayList<Service> servicesList = new ArrayList<Service>(servicesStore.values());
		return servicesList;
	}
	
	public void LoadServiceTypeList(List<String> types)
	{
		Iterator<String> it = types.iterator();
		
		while(it.hasNext())
		{
			String type = it.next();
			cmbTypes.addItem(type);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnCreate)
		{
			ServiceTypeBuilder stb = new ServiceTypeBuilder("");
			ServiceType serviceTypeTmp = stb.GetServiceType((String) cmbTypes.getSelectedItem());
			
			Service serviceTmp = new Service(serviceTypeTmp);
			addService(serviceTmp);
			
			return;
			
			//Testowe dodanie obiektu contract
			//Contract contract = new Contract(stb.GetServiceType((String) cmbTypes.getSelectedItem()));
			//addService(contract);
			 
		}
		
		
		if(e.getSource() == btnSend)
		{
			Service srv = servicesStore.get( (String)cmbServices.getSelectedItem());
			mbpManagerGui.myAgent.SendService(srv, txtAgentName.getText());
			return;
		}
		// TODO Auto-generated method stub
		
	}
	
	public void addService(Service srv)
	{
		int items = servicesStore.size();
		String name = srv.getType().getServiceTypeName() + " [" + items + "]";
		servicesStore.put(name, srv);
		cmbServices.addItem(name);
		cmbServices.setSelectedItem(name);			
	}
	
	public void loadService(Service srv)
	{
		serviceEditPanel.loadService(srv);
	}
	
	
	public boolean setAdvanceActivService(ArrayList<Resource> resourcesList)
	{
		return service.addResourcesAdvance(resourcesList);
	}
	
	public boolean setStuffActivService(ArrayList<Resource> resourcesList)
	{
		return service.addResourcesStuff(resourcesList);
	}
	
	public ArrayList<Resource> executeActivService(ArrayList<Resource> resourcesList)
	{	
		return (ArrayList<Resource>) service.executeService(resourcesList);
	}
}
